﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace CodeAuto.DALS
{
    public class IO
    {
        /// <summary>
        /// 复制指定目录的所有文件,包含子目录
        /// </summary>
        /// <param name="sourceDir">原始目录</param>
        /// <param name="targetDir">目标目录</param>
        /// <param name="overWrite">如果为true,覆盖同名文件,否则不覆盖</param>
        public static void CopyFiles(string sourceDir, string targetDir, bool overWrite)
        {
            if (!Directory.Exists(targetDir))
            {
                Directory.CreateDirectory(targetDir);
            }
            foreach (string sourceFileName in Directory.GetFiles(sourceDir))
            {
                string tempname = sourceFileName.Substring(sourceFileName.LastIndexOf("/") + 1);
                System.IO.File.Copy(sourceFileName, targetDir + tempname, overWrite);
            }
            System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(sourceDir);
            System.IO.DirectoryInfo[] ds = d.GetDirectories("*.*", System.IO.SearchOption.TopDirectoryOnly);
            if (ds.Length > 0)
            {
                foreach (System.IO.DirectoryInfo var in ds)
                {
                    string FullName = var.FullName.Replace("\\", "/");
                    string Name = var.Name;
                    CopyFiles(FullName + "/", targetDir + Name + "/", overWrite);
                }
            }
        }
    }
}